﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;


namespace BLL
{
    public class CardBLL:ICardBLL
    {

        ////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////
        public bool BatchExportData(List<Model.Cards> lists, string saveFileName)
        {
            bool validator = false;

            if (0 != lists.Count)
            {
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

                if (null != xlApp)
                {
                    Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                    Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];


                    // 写入列名
                    worksheet.Cells[1, 1] = "CD_cardId";
                    worksheet.Cells[1, 2] = "CD_cardNumber";
                    worksheet.Cells[1, 3] = "CD_cardType";
                    worksheet.Cells[1, 4] = "CD_validDate";
                    worksheet.Cells[1, 5] = "CD_used";


                    // 对应写入数值 
                    for (int r = 0; r < lists.Count; r++)
                    {
                        Model.Cards cardInfo = (Model.Cards)lists[r];
                        worksheet.Cells[r + 2, 1] = cardInfo.getCD_cardId();
                        worksheet.Cells[r + 2, 2] = cardInfo.getCD_cardNumber();
                        worksheet.Cells[r + 2, 3] = cardInfo.getCD_cardType();
                        worksheet.Cells[r + 2, 4] = cardInfo.getCD_validDate();
                        worksheet.Cells[r + 2, 5] = cardInfo.getCD_used();
                    }


                    //列宽自适应
                    worksheet.Columns.EntireColumn.AutoFit();


                    if (saveFileName != "")
                    {
                        try
                        {
                            workbook.Saved = true;
                            workbook.SaveCopyAs(saveFileName);
                            validator = true;
                        }
                        catch (Exception ex)
                        {
                            
                            validator = false;
                            throw ex;
                        }
                    }
                    else
                    {
                        validator = false;
                    }


                    xlApp.Quit();


                    // 强制销毁
                    GC.Collect();

                }
            }
            return validator;
        }
        ////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////


        public string GenerateCheckCode(int codeCount)
        {
            string str = string.Empty;
            int rep = 0;
            long num2 = DateTime.Now.Ticks + rep;
            rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
            for (int i = 0; i < codeCount; i++)
            {
                char ch;
                int num = random.Next();
                if ((num % 2) == 0)
                {
                    ch = (char)(0x30 + ((ushort)(num % 10)));
                }
                else
                {
                    ch = (char)(0x41 + ((ushort)(num % 0x1a)));
                }
                str = str + ch.ToString();
            }
            return str;
        }

        public List<Model.Cards> generateCards(int number, int type, DateTime validateTime)
        {
            List<Model.Cards> cardList = new List<Model.Cards>();  
         
            string str = GenerateCheckCode(8);
            
            DAL.CardsDAL cardDal = (DAL.CardsDAL)DALFAC.DataAccess.CreateCards();

            if (null != cardDal)
            {
                for (int i = 0; i < number; i++)
                {
                    Model.Cards cardModel = new Model.Cards();
                    cardModel.setCD_cardType(type);
                    cardModel.setCD_validDate(validateTime);
                    cardModel.setCD_used(false);

                    while (cardDal.cardNumberExists(str))
                    {
                        str = GenerateCheckCode(8);
                    }

                    cardModel.setCD_cardNumber(str);
                    if (cardDal.Add(cardModel))
                    {
                        int id = cardDal.GetModelByCradNum(cardModel.getCD_cardNumber()).getCD_cardId();
                        cardModel.setCD_cardId(id);
                        cardList.Add(cardModel); 
                    }
                }
            }

            return cardList;
        }

        public bool addType(Model.CardType newType)
        {
            bool validator = false;
            DAL.CardTypeDAL cardTypeDal = (DAL.CardTypeDAL)DALFAC.DataAccess.CreateCardType();
            if (null != cardTypeDal)
            {
                if (!(cardTypeDal.cardTypeExists(newType.getCT_cardType())))
                {
                    validator = cardTypeDal.Add(newType);
                }
            }

            return validator;
        }

        public List<Model.CardType> getTypeList()
        {

            List<Model.CardType> cardTypeList = new List<Model.CardType>(); 
            DAL.CardTypeDAL cardTypeDal = (DAL.CardTypeDAL)DALFAC.DataAccess.CreateCardType();
            if (null != cardTypeDal)
            {
                
                cardTypeList = cardTypeDal.getTypeList();
              
            }

            return cardTypeList;
        }

        public bool generateReport(List<Model.Cards> list)
        {
            bool validator = false;
            BatchExportData(list, "d:\\Project\\tiamat.xlsx");
            return validator;
        }
    }
}
